package com.xoa.service.flowhook.Impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

import com.xoa.dao.workflow.FlowTypeModelMapper;
import com.xoa.model.users.Users;
import com.xoa.model.workflow.FlowDetailInfo;
import com.xoa.model.workflow.FlowTypeModel;
import com.xoa.util.CookiesUtil;
import com.xoa.util.DateFormat;
import com.xoa.util.ToJson;
import com.xoa.util.common.StringUtils;
import com.xoa.util.common.session.SessionUtils;
import org.springframework.stereotype.Service;

import com.xoa.dao.flowhook.FlowHookMapper;
import com.xoa.model.workflow.FlowHookWithBLOBs;
import com.xoa.service.flowhook.FlowHookService;
import org.springframework.transaction.annotation.Transactional;

@Service
public class FlowHookServiceImpl implements FlowHookService {
    @Resource
    private FlowHookMapper flowHookMapper;
    @Resource
    private FlowTypeModelMapper flowTypeModelMapper;

    @Override
    @Transactional
    public int saveHook(FlowHookWithBLOBs flowHook) {
        return flowHookMapper.insertSelective(flowHook);
    }

    @Override
    @Transactional
    public int deleteHook(FlowHookWithBLOBs flowHook) {
        return flowHookMapper.deleteByhid(flowHook);
    }

    @Override
    @Transactional
    public int updateHook(FlowHookWithBLOBs flowHook) {
        return flowHookMapper.updateByhidSelective(flowHook);
    }

    @Override
    public FlowHookWithBLOBs getHookById(FlowHookWithBLOBs flowHook) {
        return flowHookMapper.selectByhid(flowHook);
    }

    @Override
    public List<FlowHookWithBLOBs> getHookAll(FlowHookWithBLOBs flowHook) {
        List<FlowHookWithBLOBs> flowHookWithBLOBs = flowHookMapper.gethookAll(flowHook);
        for (FlowHookWithBLOBs f : flowHookWithBLOBs) {
            Integer flowId = f.getFlowId();
            String flwoName=null;
            if (flowId != null) {
                flwoName= flowTypeModelMapper.getFlowName(flowId);
                if( null!=flwoName  && !"".equals(flwoName)){
                    f.setFlowName(flwoName);
                }
            }
        }
        return flowHookWithBLOBs;
    }

    @Override
    public FlowHookWithBLOBs selectByOrderId(FlowHookWithBLOBs flowHookWithBLOBs) {
        return flowHookMapper.selectByOrderId(flowHookWithBLOBs);
    }

    @Override
    public FlowHookWithBLOBs selectByName(FlowHookWithBLOBs flowHookWithBLOBs) {
        return flowHookMapper.selectByName(flowHookWithBLOBs);
    }

    @Override
    public ToJson<FlowHookWithBLOBs> queryFormId(String folwId) {
        ToJson<FlowHookWithBLOBs> flowHookWithBLOBsToJson=new ToJson<>();
        if(StringUtils.checkNull(folwId)){
            flowHookWithBLOBsToJson.setFlag(1);
            flowHookWithBLOBsToJson.setMsg("err");
            return  flowHookWithBLOBsToJson;
        }
        FlowTypeModel flowTypeModel=flowTypeModelMapper.queryOneObject(Integer.parseInt(folwId));
        if(flowTypeModel!=null){
            flowHookWithBLOBsToJson.setFlag(0);
            flowHookWithBLOBsToJson.setMsg("ok");
            flowHookWithBLOBsToJson.setObject(flowTypeModel);
        }else {
            flowHookWithBLOBsToJson.setFlag(1);
            flowHookWithBLOBsToJson.setMsg("err");
        }



        return flowHookWithBLOBsToJson;
    }

    /**
     * 流程 门户上面的统计按照业务应用 查找接口和流程ID
     * @param request
     * @return
     */
    @Override
    public ToJson queryCountDataByQuarter(HttpServletRequest request) {
        ToJson toJson=new ToJson(1,"false");
        try {
            Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
            Users user = SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users(), redisSessionCookie);

            Map map=new HashMap();

            SimpleDateFormat longSdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            //季度的查询开始时间和结束时间
            String QuarterStartTime = longSdf.format(DateFormat.getCurrentQuarterStartTime());
            String QuarterEndTime = longSdf.format(DateFormat.getCurrentQuarterEndTime());

            //月度的开始时间和结束时间
            String MonthStartTime = longSdf.format(DateFormat.getCurrentMonthStartTime());
            String MonthEndTime = longSdf.format(DateFormat.getCurrentMonthEndTime());

            //查询季度的请假
            map.put("本季度请假",flowHookMapper.quarter(user.getUserId(),QuarterStartTime,QuarterEndTime,"attend_leave"));
            map.put("本季度加班",flowHookMapper.quarterFlowId(user.getUserId(),QuarterStartTime,QuarterEndTime,"201"));
            map.put("本季度出差",flowHookMapper.quarter(user.getUserId(),QuarterStartTime,QuarterEndTime,"attend_evection"));

            //月度
            map.put("本月请假",flowHookMapper.quarter(user.getUserId(),MonthStartTime,MonthEndTime,"attend_leave"));
            map.put("本月加班",flowHookMapper.quarterFlowId(user.getUserId(),MonthStartTime,MonthEndTime,"201"));
            map.put("本月出差",flowHookMapper.quarter(user.getUserId(),MonthStartTime,MonthEndTime,"attend_evection"));

            toJson.setMsg("ok");
            toJson.setFlag(0);
            toJson.setObject(map);
        }catch (Exception e){
            e.printStackTrace();
            toJson.setMsg(e.getMessage());
        }
        return toJson;
    }

}
